$(document).ready(function(){
    $.ajax({
        type: 'post',
        url: 'http://localhost:8066/company/selectAll',
        data: '',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            console.log(result)
             // var result = eval("(" + json +")");
            // console.log(result.data[1].name);
            var html = "";
            for(var i in result.data){
                html += "<tr>\n" +
                    "\t\t\t\t\t\t\t\t\t\t<td>"+ result.data[i].id +"</td>\n" +
                    "\t\t\t\t\t\t\t\t\t\t<td>"+ result.data[i].name+"</td>\n" +
                    "\t\t\t\t\t\t\t\t\t\t<td>" + result.data[i].address +"</td>\n" +
                    "\t\t\t\t\t\t\t\t\t\t<td>" + result.data[i].adminId +"</td>\n" +
                    "\t\t\t\t\t\t\t\t\t\t<td>\n" +
                    "\t\t\t\t\t\t\t\t\t\t\t<a href=\"javascript:void(0)\" onclick='editComp(this)'>修改</a>\n" +
                    "\t\t\t\t\t\t\t\t\t\t\t<a href=\"javascript:void(0)\" onclick='delCom(this)'>删除</a>\n" +
                    "\t\t\t\t\t\t\t\t\t\t</td>\n" +
                    "\t\t\t\t\t\t\t\t\t</tr>"
            }
            $("#companyTable tbody").html(html);
        }
    });

})

function  editComp(obj) {
    var tds= $(obj).parent().parent().find('td');
    $('#mdyComname').val(tds.eq(1).text());
    // $('#mdysex').val(tds.eq(1).text());
    $('#mdyComadd').val(tds.eq(2).text());
    $("#mdyComId").val(tds.eq(0).text());
    $('#modifyCompany').modal('show');
}
//提交更改
function update() {
    //获取模态框数据
    var name = $('#mdyComname').val();
    var add = $('#mdyComadd').val();
    var id = $('#mdyComId').val();
    //console.log(id+" "+add);
    var str = '{"id":"'+ id+'", "name":"'+ name+'","address":"'+ add+'" }';
    console.log(str);
        $.ajax({
        type: "post",
        url: "http://localhost:8066/company/update",
        data: str,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            $("#companyTable").find("tr").eq(id).find("td").eq(1).html($('#mdyComname').val());
            $("#companyTable").find("tr").eq(id).find("td").eq(2).html($('#mdyComadd').val())
        }
    });
    $('#modifyCompany').modal('hide');
}

//删除
function delCom(obj){
    var id = $(obj).parent().parent().find("td").eq(0).html();
    console.log(id)
    var str = '{"id":"'+id+'"}';
    $.ajax({
        type: "post",
        url: "http://localhost:8066/company/delete",
        data: str,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            location.reload();
        }
    });
}

function addCom(){
    var name = $("#companyName").val();
    var addr = $("#companyAdd").val();
    console.log(name);
    var str = '{"name":"'+ name+'","address":"'+ addr+'" }';
    $.ajax({
        type: "post",
        url: "http://localhost:8066/company/add",
        data: str,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(result) {
            location.reload();
        }
    });
}